Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "scale_linear" preprocessing operation #155

Merged
merged 1 commit into from
Mar 26, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@
from tiktorch.server.prediction_pipeline._preprocessing import ADD_BATCH_DIM, make_preprocessing


def test_scale_linear():
spec = Preprocessing(name="scale_linear", kwargs={"offset": 42, "gain": 2})
data = xr.DataArray(np.arange(4).reshape(2, 2), dims=("x", "y"))
expected = xr.DataArray(np.array([[42, 44], [46, 48]]), dims=("x", "y"))
preprocessing = make_preprocessing([spec])
result = preprocessing(data)
xr.testing.assert_allclose(expected, result)


def test_zero_mean_unit_variance_preprocessing():
zero_mean_spec = Preprocessing(name="zero_mean_unit_variance", kwargs={})
data = xr.DataArray(np.arange(9).reshape(3, 3), dims=("x", "y"))
Expand Down
5 changes: 5 additions & 0 deletions tiktorch/server/prediction_pipeline/_preprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ def make_ensure_dtype_preprocessing(dtype):
return Preprocessing(name="__tiktorch_ensure_dtype", kwargs={"dtype": dtype})


def scale_linear(tensor: xr.DataArray, *, gain, offset) -> xr.DataArray:
return gain * tensor + offset
Copy link
Collaborator

@k-dominik k-dominik Mar 22, 2021

Choose a reason for hiding this comment

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

what happens to overflows here? (what is supposed to happen?) Or maybe rephrase, is it okay to change dtype here?

Copy link
Member Author

@emilmelnikov emilmelnikov Mar 22, 2021

Choose a reason for hiding this comment

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

Good catch, but I think we should address this question at the spec level. That is, any decision will be easy to implement, but it needs to be consistent with the other spec implementors.

is it okay to change dtype here?

I think we have to do it anyway: gain and offset might be floats.

Copy link
Member Author

Choose a reason for hiding this comment

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

The current implementation casts input to a model dtype before doing any preprocessing, which is probably float32 in ~99% cases.



def zero_mean_unit_variance(tensor: xr.DataArray, axes=None, eps=1.0e-6, mode="per_sample") -> xr.DataArray:
if axes:
axes = tuple(axes)
Expand Down Expand Up @@ -40,6 +44,7 @@ def add_batch_dim(tensor: xr.DataArray):


KNOWN_PREPROCESSING = {
"scale_linear": scale_linear,
"zero_mean_unit_variance": zero_mean_unit_variance,
"__tiktorch_add_batch_dim": add_batch_dim,
"__tiktorch_ensure_dtype": ensure_dtype,
Expand Down