aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/tensorboard/components/tf-graph-common/lib/parser.ts
blob: b4864738a96f8cd0a0a3e80b78f0470702329660 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/// <reference path="../../../typings/tsd.d.ts" />
/// <reference path="common.ts" />
module tf.graph.parser {

/**
 * Parses a native js value, which can be either a string, boolean or number.
 *
 * @param value The value to be parsed.
 */
function parseValue(value: string): string|number|boolean {
  if (value === "true") {
    return true;
  }
  if (value === "false") {
    return false;
  }
  let firstChar = value[0];
  if (firstChar === "\"") {
    return value.substring(1, value.length - 1);
  }
  let num = parseFloat(value);
  return isNaN(num) ? value : num;
}

/**
 * Fetches a text file and returns a promise of the result.
 */
export function readPbTxt(filepath: string): Promise<string> {
  return new Promise<string>(function(resolve, reject) {
    d3.text(filepath, function(error, text) {
      if (error) {
        reject(error);
        return;
      }
      resolve(text);
    });
  });
}

/**
 * Fetches and parses a json file and returns a promise of the result.
 */
export function readJson(filepath: string): Promise<Object> {
  return new Promise<Object>(function(resolve, reject) {
    d3.json(filepath, function(error, text) {
      if (error) {
        reject(error);
        return;
      }
      resolve(text);
    });
  });
}

/**
 * Reads the graph and stats file (if available), parses them and returns a
 * promise of the result.
 */
export function readAndParseData(dataset: {path: string, statsPath: string},
    pbTxtContent: string, tracker: ProgressTracker):
    Promise<{ nodes: TFNode[], statsJson: Object }|void> {
  let graphPbTxt;
  let statsJson;
  return runAsyncTask("Reading graph.pbtxt", 20, () => {
    return pbTxtContent || readPbTxt(dataset.path);
  }, tracker)
  .then(function(text) {
    graphPbTxt = text;
    return runAsyncTask("Reading stats.pbtxt", 20, () => {
      return (dataset != null && dataset.statsPath != null) ?
          readJson(dataset.statsPath) : null;
    }, tracker);
  })
  .then(function(json) {
    statsJson = json;
    return runAsyncTask("Parsing graph.pbtxt", 60, () => {
      return parsePbtxt(graphPbTxt);
    }, tracker);
  })
  .then(function(nodes) {
    return {
      nodes: nodes,
      statsJson: statsJson
    };
    })
  .catch(function(reason) {
    throw new Error("Failure parsing graph definition");
  });
}

/**
 * Parses a proto txt file into a javascript object.
 *
 * @param input The string contents of the proto txt file.
 * @return The parsed object.
 */
export function parsePbtxt(input: string): TFNode[] {
  let output: { [name: string]: any; } = { node: [] };
  let stack = [];
  let path: string[] = [];
  let current: { [name: string]: any; } = output;

  function splitNameAndValueInAttribute(line: string) {
    let colonIndex = line.indexOf(":");
    let name = line.substring(0, colonIndex).trim();
    let value = parseValue(line.substring(colonIndex + 2).trim());
    return {
      name: name,
      value: value
    };
  }

  /**
   * Since proto-txt doesn't explicitly say whether an attribute is repeated
   * (an array) or not, we keep a hard-coded list of attributes that are known
   * to be repeated. This list is used in parsing time to convert repeated
   * attributes into arrays even when the attribute only shows up once in the
   * object.
   */
  let ARRAY_ATTRIBUTES: {[attrPath: string] : boolean} = {
    "node": true,
    "node.input": true,
    "node.attr": true,
    "node.attr.value.list.type": true,
    "node.attr.value.shape.dim": true,
    "node.attr.value.tensor.string_val": true,
    "node.attr.value.tensor.tensor_shape.dim": true
  };

  /**
   * Adds a value, given the attribute name and the host object. If the
   * attribute already exists, but is not an array, it will convert it to an
   * array of values.
   *
   * @param obj The host object that holds the attribute.
   * @param name The attribute name (key).
   * @param value The attribute value.
   * @param path A path that identifies the attribute. Used to check if
   *     an attribute is an array or not.
   */
  function addAttribute(obj: Object, name: string,
      value: Object|string|number|boolean, path: string[]): void {
    // We treat "node" specially since it is done so often.
    let existingValue = obj[name];
    if (existingValue == null) {
      obj[name] = path.join(".") in ARRAY_ATTRIBUTES ? [value] : value;
    } else if (Array.isArray(existingValue)) {
      existingValue.push(value);
    } else {
      obj[name] = [existingValue, value];
    }
  }

  // Run through the file a line at a time.
  let startPos = 0;
  while (startPos < input.length) {
    let endPos = input.indexOf("\n", startPos);
    if (endPos === -1) {
      endPos = input.length;
    }
    let line = input.substring(startPos, endPos);
    startPos = endPos + 1;
    if (!line) {
      continue;
    }
    switch (line[line.length - 1]) {
      case "{": // create new object
        let name = line.substring(0, line.length - 2).trim();
        let newValue: { [name: string]: any; } = {};
        stack.push(current);
        path.push(name);
        addAttribute(current, name, newValue, path);
        current = newValue;
        break;
      case "}":
        current = stack.pop();
        path.pop();
        break;
      default:
        let x = splitNameAndValueInAttribute(line);
        addAttribute(current, x.name, x.value, path.concat(x.name));
        break;
    }
  }

  return output["node"];
}

} // Close module tf.graph.parser.