Skip to content
This repository was archived by the owner on Aug 15, 2019. It is now read-only.

use precomputed tensor size #1608

Merged
merged 1 commit into from
Mar 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ export class Engine implements TensorManager, TensorTracker, DataMover {
// string tensors are counted when writing values.
let bytes = 0;
if (a.dtype !== 'complex64' && a.dtype !== 'string') {
bytes = util.sizeFromShape(a.shape) * util.bytesPerElement(a.dtype);
bytes = a.size * util.bytesPerElement(a.dtype);
}
this.tensorInfo.set(a.dataId, {
backend: backend != null ? backend : this.backend,
Expand Down
6 changes: 3 additions & 3 deletions src/kernels/backend_cpu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2740,7 +2740,7 @@ export class MathBackendCPU implements KernelBackend {
const channels = x.shape[3];
const maxD = channels - 1;
const xValues = x.dataSync();
const size = util.sizeFromShape(x.shape);
const size = x.size;
const result = new Float32Array(size);

function sumAcrossChannels(offset: number) {
Expand Down Expand Up @@ -2776,8 +2776,8 @@ export class MathBackendCPU implements KernelBackend {
const dyValues = dy.dataSync();
const inputImageValues = inputImage.dataSync();
const outputImageValues = outputImage.dataSync();
const result = new Float32Array(util.sizeFromShape(dy.shape));
const size = util.sizeFromShape(dy.shape);
const result = new Float32Array(dy.size);
const size = dy.size;

for (let offset = 0; offset < size; offset++) {
const currentChannel = offset % channels;
Expand Down
4 changes: 2 additions & 2 deletions src/ops/loss_ops.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {customGrad} from '../globals';
import {Tensor} from '../tensor';
import {convertToTensor} from '../tensor_util_env';
import {TensorLike} from '../types';
import {assertShapesMatch, sizeFromShape} from '../util';
import {assertShapesMatch} from '../util';

import {expandShapeToKeepDim} from './axis_util';

Expand Down Expand Up @@ -66,7 +66,7 @@ function computeWeightedLoss_<T extends Tensor, O extends Tensor>(
return weightedLoss.mean();
} else {
const broadcastFactor =
sizeFromShape($losses.shape) / sizeFromShape($weights.shape);
$losses.size / $weights.size;
const result = weightedLoss.sum().div($weights.sum());
return broadcastFactor > 1 ? result.div(scalar(broadcastFactor)) :
result as O;
Expand Down
2 changes: 1 addition & 1 deletion src/tensor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export class TensorBuffer<R extends Rank, D extends DataType = 'float32'> {
`call tf.complex(real, imag).`);
}
this.values =
values || util.getArrayFromDType(dtype, util.sizeFromShape(this.shape));
values || util.getArrayFromDType(dtype, this.size);
this.strides = computeStrides(shape);
}

Expand Down