Skip to content
This repository was archived by the owner on Jun 14, 2024. It is now read-only.
Open
7 changes: 7 additions & 0 deletions docs/supported_ops.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,13 @@
|Not mapped|ifft|
|Not mapped|rfft|

## Operations - Strings

|Tensorflow Op Name|Tensorflow.js Op Name|
|---|---|
|DecodeBase64|decodeBase64|
|EncodeBase64|encodeBase64|

## Tensors - Transformations

|Tensorflow Op Name|Tensorflow.js Op Name|
Expand Down
46 changes: 46 additions & 0 deletions src/operations/executors/string_executor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* @license
* Copyright 2018 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/

import * as tfc from '@tensorflow/tfjs-core';

import {NamedTensorsMap} from '../../data/types';
import {ExecutionContext} from '../../executor/execution_context';
import {InternalOpExecutor, Node} from '../types';

import {getParamValue} from './utils';

export let executeOp: InternalOpExecutor =
(node: Node, tensorMap: NamedTensorsMap,
context: ExecutionContext): tfc.Tensor[] => {
switch (node.op) {
case 'DecodeBase64': {
const input =
getParamValue('str', node, tensorMap, context) as tfc.Tensor;
return [tfc.decodeBase64(input)];
}
case 'EncodeBase64': {
const input =
getParamValue('str', node, tensorMap, context) as tfc.Tensor;
const pad = getParamValue('pad', node, tensorMap, context) as boolean;
return [tfc.encodeBase64(input, pad)];
}
default:
throw TypeError(`Node type ${node.op} is not implemented`);
}
};

export const CATEGORY = 'string';
64 changes: 64 additions & 0 deletions src/operations/executors/string_executor_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* @license
* Copyright 2018 Google Inc. All Rights Reserved.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

update to 2019 for all files, thanks.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated (c9ba5f6, fb065c7)

* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/
import * as tfc from '@tensorflow/tfjs-core';

import {ExecutionContext} from '../../executor/execution_context';
import {Node} from '../types';

import {executeOp} from './string_executor';
// tslint:disable-next-line:max-line-length

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not needed.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed (c40b65c)

import {createBoolAttr, createTensorAttr} from './test_helper';

describe('string', () => {
let node: Node;
const input1 = [tfc.tensor(['a'], [1], 'string')];
const context = new ExecutionContext({}, {});

beforeEach(() => {
node = {
name: 'test',
op: '',
category: 'string',
inputNames: ['input1'],
inputs: [],
inputParams: {str: createTensorAttr(0)},
attrParams: {},
children: []
};
});

describe('executeOp', () => {
describe('DecodeBase64', () => {
it('should call tfc.decodeBase64', () => {
spyOn(tfc, 'decodeBase64');
node.op = 'DecodeBase64';
executeOp(node, {input1}, context);
expect(tfc.decodeBase64).toHaveBeenCalledWith(input1[0]);
});
});
describe('EncodeBase64', () => {
it('should call tfc.encodeBase64', () => {
spyOn(tfc, 'encodeBase64');
node.op = 'EncodeBase64';
node.attrParams.pad = createBoolAttr(true);
executeOp(node, {input1}, context);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove the empty line here.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done (7663a37)

expect(tfc.encodeBase64).toHaveBeenCalledWith(input1[0], true);
});
});
});
});
32 changes: 32 additions & 0 deletions src/operations/op_list/string.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import {OpMapper} from '../types';

/**
* @license
* Copyright 2018 Google LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/

export const json: OpMapper[] = [
{
'tfOpName': 'DecodeBase64',
'category': 'string',
'inputs': [{'start': 0, 'name': 'input', 'type': 'tensor'}]
},
{
'tfOpName': 'EncodeBase64',
'category': 'string',
'inputs': [{'start': 0, 'name': 'input', 'type': 'tensor'}],
'attrs': [{'tfName': 'pad', 'name': 'pad', 'type': 'bool'}]
}
];
3 changes: 2 additions & 1 deletion src/operations/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ export type ParamType = 'number'|'string'|'string[]'|'number[]'|'bool'|'bool[]'|
export type Category =
'arithmetic'|'basic_math'|'control'|'convolution'|'custom'|'dynamic'|
'evaluation'|'image'|'creation'|'graph'|'logical'|'matrices'|
'normalization'|'reduction'|'slice_join'|'spectral'|'transformation';
'normalization'|'reduction'|'slice_join'|'spectral'|'string'|
'transformation';

// For mapping input or attributes of NodeDef into TensorFlow.js op param.
export declare interface ParamMapper {
Expand Down