|
| 1 | +/* Copyright 2023 The TensorFlow Authors. All Rights Reserved. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. |
| 14 | +==============================================================================*/ |
| 15 | +import {Injectable} from '@angular/core'; |
| 16 | +import {Observable} from 'rxjs'; |
| 17 | +import {map} from 'rxjs/operators'; |
| 18 | + |
| 19 | +import { |
| 20 | + Domain, |
| 21 | + DomainType, |
| 22 | + BackendListSessionGroupRequest, |
| 23 | + BackendHparamsExperimentResponse, |
| 24 | + BackendHparamSpec, |
| 25 | + DiscreteDomainHparamSpec, |
| 26 | + SessionGroup, |
| 27 | + HparamAndMetricSpec, |
| 28 | + IntervalDomainHparamSpec, |
| 29 | + BackendListSessionGroupResponse, |
| 30 | + RunStatus, |
| 31 | +} from '../types'; |
| 32 | +import {TBHttpClient} from '../../webapp_data_source/tb_http_client'; |
| 33 | + |
| 34 | +const HPARAMS_HTTP_PATH_PREFIX = 'data/plugin/hparams'; |
| 35 | + |
| 36 | +function isHparamDiscrete( |
| 37 | + hparam: BackendHparamSpec |
| 38 | +): hparam is DiscreteDomainHparamSpec { |
| 39 | + return Boolean((hparam as DiscreteDomainHparamSpec).domainDiscrete); |
| 40 | +} |
| 41 | + |
| 42 | +function isHparamInterval( |
| 43 | + hparam: BackendHparamSpec |
| 44 | +): hparam is IntervalDomainHparamSpec { |
| 45 | + return Boolean((hparam as IntervalDomainHparamSpec).domainInterval); |
| 46 | +} |
| 47 | + |
| 48 | +function getHparamDomain(hparam: BackendHparamSpec): Domain { |
| 49 | + if (isHparamDiscrete(hparam)) { |
| 50 | + return { |
| 51 | + type: DomainType.DISCRETE, |
| 52 | + values: hparam.domainDiscrete, |
| 53 | + }; |
| 54 | + } |
| 55 | + |
| 56 | + if (isHparamInterval(hparam)) { |
| 57 | + return { |
| 58 | + ...hparam.domainInterval, |
| 59 | + type: DomainType.INTERVAL, |
| 60 | + }; |
| 61 | + } |
| 62 | + |
| 63 | + return { |
| 64 | + values: [], |
| 65 | + type: DomainType.DISCRETE, |
| 66 | + }; |
| 67 | +} |
| 68 | + |
| 69 | +@Injectable() |
| 70 | +export class HparamsDataSource { |
| 71 | + constructor(private readonly http: TBHttpClient) {} |
| 72 | + |
| 73 | + private getPrefix(experimentIds: string[]) { |
| 74 | + return experimentIds.length > 1 ? 'compare' : 'experiment'; |
| 75 | + } |
| 76 | + |
| 77 | + private formatExperimentIds(experimentIds: string[]) { |
| 78 | + if (experimentIds.length === 1) { |
| 79 | + return experimentIds[0]; |
| 80 | + } |
| 81 | + |
| 82 | + // The server does not send back experiment ids. Instead the response is formatted as |
| 83 | + // `[AliasNumber] ExperimentAlias/RunName` |
| 84 | + // By using the index as the alias we can translate associate the response with an experiment id |
| 85 | + // Note: The experiment id itself cannot be the alias because it may contain ':' |
| 86 | + return experimentIds.map((eid, index) => `${index}:${eid}`).join(','); |
| 87 | + } |
| 88 | + |
| 89 | + fetchExperimentInfo( |
| 90 | + experimentIds: string[] |
| 91 | + ): Observable<HparamAndMetricSpec> { |
| 92 | + const formattedExperimentIds = this.formatExperimentIds(experimentIds); |
| 93 | + return this.http |
| 94 | + .post<BackendHparamsExperimentResponse>( |
| 95 | + `/${this.getPrefix( |
| 96 | + experimentIds |
| 97 | + )}/${formattedExperimentIds}/${HPARAMS_HTTP_PATH_PREFIX}/experiment`, |
| 98 | + {experimentName: formattedExperimentIds}, |
| 99 | + {}, |
| 100 | + 'request' |
| 101 | + ) |
| 102 | + .pipe( |
| 103 | + map((response) => { |
| 104 | + return { |
| 105 | + hparams: response.hparamInfos.map((hparam) => { |
| 106 | + const feHparam = { |
| 107 | + ...hparam, |
| 108 | + domain: getHparamDomain(hparam), |
| 109 | + }; |
| 110 | + |
| 111 | + delete (feHparam as any).domainInterval; |
| 112 | + delete (feHparam as any).domainDiscrete; |
| 113 | + |
| 114 | + return feHparam; |
| 115 | + }), |
| 116 | + metrics: response.metricInfos.map((metric) => ({ |
| 117 | + ...metric, |
| 118 | + tag: metric.name.tag, |
| 119 | + })), |
| 120 | + }; |
| 121 | + }) |
| 122 | + ); |
| 123 | + } |
| 124 | + |
| 125 | + fetchSessionGroups( |
| 126 | + experimentIds: string[], |
| 127 | + hparamsAndMetricsSpecs: HparamAndMetricSpec |
| 128 | + ): Observable<SessionGroup[]> { |
| 129 | + const formattedExperimentIds = this.formatExperimentIds(experimentIds); |
| 130 | + |
| 131 | + const colParams: BackendListSessionGroupRequest['colParams'] = []; |
| 132 | + |
| 133 | + for (const hparam of hparamsAndMetricsSpecs.hparams) { |
| 134 | + colParams.push({hparam: hparam.name}); |
| 135 | + } |
| 136 | + for (const mectric of hparamsAndMetricsSpecs.metrics) { |
| 137 | + colParams.push({ |
| 138 | + metric: mectric.name, |
| 139 | + }); |
| 140 | + } |
| 141 | + |
| 142 | + const listSessionRequestParams: BackendListSessionGroupRequest = { |
| 143 | + experimentName: formattedExperimentIds, |
| 144 | + allowedStatuses: [ |
| 145 | + RunStatus.STATUS_FAILURE, |
| 146 | + RunStatus.STATUS_RUNNING, |
| 147 | + RunStatus.STATUS_SUCCESS, |
| 148 | + RunStatus.STATUS_UNKNOWN, |
| 149 | + ], |
| 150 | + colParams, |
| 151 | + startIndex: 0, |
| 152 | + // arbitrary large number so it does not get clipped. |
| 153 | + sliceSize: 1e6, |
| 154 | + }; |
| 155 | + |
| 156 | + return this.http |
| 157 | + .post<BackendListSessionGroupResponse>( |
| 158 | + `/${this.getPrefix( |
| 159 | + experimentIds |
| 160 | + )}/${formattedExperimentIds}/${HPARAMS_HTTP_PATH_PREFIX}/session_groups`, |
| 161 | + listSessionRequestParams, |
| 162 | + {}, |
| 163 | + 'request' |
| 164 | + ) |
| 165 | + .pipe( |
| 166 | + map((response) => |
| 167 | + response.sessionGroups.map((sessionGroup) => { |
| 168 | + sessionGroup.sessions = sessionGroup.sessions.map((session) => { |
| 169 | + /* |
| 170 | + * In single experiment mode the Session.name is equal to the runName. |
| 171 | + * In comparison view it is `[AliasNumber] ExperimentAlias/runName` |
| 172 | + * |
| 173 | + * We store runs as experimentId/runName so it is necessary to prepend the experiment name |
| 174 | + * in single experiment view. "In comparison view we pass the indeces of the experimentIds |
| 175 | + * as the aliases in the request. That allows us to parse the indeces from the response and |
| 176 | + * use them to lookup the correct ids from the experimentIds argument. |
| 177 | + */ |
| 178 | + if (experimentIds.length > 1) { |
| 179 | + const [, ...aliasAndRunName] = session.name.split(' '); |
| 180 | + const [experimentIndex, ...runName] = aliasAndRunName |
| 181 | + .join(' ') |
| 182 | + .split('/'); |
| 183 | + session.name = [ |
| 184 | + // This parseInt should not be necessary because JS Arrays DO support indexing by string |
| 185 | + experimentIds[parseInt(experimentIndex)], |
| 186 | + ...runName, |
| 187 | + ].join('/'); |
| 188 | + } else { |
| 189 | + session.name = [experimentIds[0], session.name].join('/'); |
| 190 | + } |
| 191 | + return session; |
| 192 | + }); |
| 193 | + return sessionGroup; |
| 194 | + }) |
| 195 | + ) |
| 196 | + ); |
| 197 | + } |
| 198 | +} |
0 commit comments