Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

MXNET-1295 Adding integer index support to Sequence* family of operators. #13880

Merged
merged 6 commits into from
Jan 27, 2019

Conversation

stephenrawls
Copy link
Contributor

Description

Adding ability to use int32 arrays, or any castable-to-int type, as
the sequence_length array to SequenceMask, SequenceLast, and
SequenceReverse. Previously these operaters all requred sequence_length
to be the same data type as the input array.

See MxNet Jira ticket here:
https://issues.apache.org/jira/browse/MXNET-1295

See also GitHub issues here:
#12649
dmlc/gluon-nlp#346

Test Coverage

I have tested this works by building locally and running all the Sequence* operators in python with a float32 input array and an int32 sequence_length array. I confirmed things work correct.

However when I looked at the unit tests it was not immediately clear how to add an appropriate test, because the current tests all use the check_symbolic_forward() function, which takes a single dtype for all arguments and doesn't allow clients to specify different dtypes for different input arguments.

Checklist

Essentials

Please feel free to remove inapplicable items for your PR.

  • The PR title starts with [MXNET-$JIRA_ID], where $JIRA_ID refers to the relevant JIRA issue created (except PRs with tiny changes)
  • Changes are complete (i.e. I finished coding on this PR)
  • [??] All changes have test coverage:
  • Code is well-documented:
  • To the my best knowledge, examples are either not affected by this change, or have been fixed to be compatible with this change

Adding ability to use int32 arrays, or any castable-to-int type, as
the sequence_length array to SequenceMask, SequenceLast, and
SequenceReverse. Previously these operaters all requred sequence_length
to be the same data type as the input array.

See MxNet Jira ticket here:
  https://issues.apache.org/jira/browse/MXNET-1295

See also GitHub issues here:
   apache#12649
   dmlc/gluon-nlp#346
@stu1130
Copy link
Contributor

stu1130 commented Jan 15, 2019

@mxnet-label-bot add [pr-awaiting-review]
Thanks for your contribution @stephenrawls

@marcoabreu marcoabreu added the pr-awaiting-review PR is waiting for code review label Jan 15, 2019
@szha
Copy link
Member

szha commented Jan 15, 2019

Thanks for contributing this, @stephenrawls. Could you add some test cases for using different input types? (tests/python/unittest/test_operator.py)

@stephenrawls
Copy link
Contributor Author

@szha Okay I added unit tests. As I alluded to in my original PR, it wasn't exactly clear the best way to do this, because the testing code seems to have a lot of assumptions that all argument types will be the same type, and that the only possible argument types are floats.

For example, there are assert dtype in (np.float16, np.float32, np.float64) sprinkled all throughout the unit test framework, and I don't know what other assumptions I'll break if I change any of that.

Also, the unit tests are relying on test_utils.py to convert all input arrays to a single input type, e.g. here is an example:

    location = {k: mx.nd.array(v, ctx=ctx, dtype=dtype) if isinstance(v, np.ndarray) \
               else v for k, v in location.items()}

I ended up modifying the logic so that I could control the dtype of each array independently by just creating the original numpy array to be the data type that I want, and telling the code to keep the numpy dtype when converting to mxnet.ndarray:

    location = {k: mx.nd.array(v, ctx=ctx, dtype=v.dtype if dtype == "asnumpy" else dtype) if isinstance(v, np.ndarray) \
               else v for k, v in location.items()}

If you want to use the new behavior, you have to opt-in and pass "asnumpy" as the dtype.

Take a look, and if you prefer a different approach just let me know.

Best,
Stephen

@szha
Copy link
Member

szha commented Jan 16, 2019

@stephenrawls thanks! the approach sounds reasonable.

@szha szha merged commit da5242b into apache:master Jan 27, 2019
@szha
Copy link
Member

szha commented Jan 27, 2019

@stephenrawls thanks for the contribution!

jessr92 pushed a commit to jessr92/incubator-mxnet that referenced this pull request Jan 27, 2019
…ors. (apache#13880)

* Adding integer index support to Sequence* family of operators.

Adding ability to use int32 arrays, or any castable-to-int type, as
the sequence_length array to SequenceMask, SequenceLast, and
SequenceReverse. Previously these operaters all requred sequence_length
to be the same data type as the input array.

See MxNet Jira ticket here:
  https://issues.apache.org/jira/browse/MXNET-1295

See also GitHub issues here:
   apache#12649
   dmlc/gluon-nlp#346

* Adding explicit braces to an if statement to fix g++ warning

* fixing sequence_mask.cu by adding IType to template

* Fixing whitespace errors reported by linter

* Adding unit tests

* Fixing length of lines to pass linter
jessr92 pushed a commit to jessr92/incubator-mxnet that referenced this pull request Jan 27, 2019
…ors. (apache#13880)

* Adding integer index support to Sequence* family of operators.

Adding ability to use int32 arrays, or any castable-to-int type, as
the sequence_length array to SequenceMask, SequenceLast, and
SequenceReverse. Previously these operaters all requred sequence_length
to be the same data type as the input array.

See MxNet Jira ticket here:
  https://issues.apache.org/jira/browse/MXNET-1295

See also GitHub issues here:
   apache#12649
   dmlc/gluon-nlp#346

* Adding explicit braces to an if statement to fix g++ warning

* fixing sequence_mask.cu by adding IType to template

* Fixing whitespace errors reported by linter

* Adding unit tests

* Fixing length of lines to pass linter
haohuanw pushed a commit to haohuanw/incubator-mxnet that referenced this pull request Jun 23, 2019
…ors. (apache#13880)

* Adding integer index support to Sequence* family of operators.

Adding ability to use int32 arrays, or any castable-to-int type, as
the sequence_length array to SequenceMask, SequenceLast, and
SequenceReverse. Previously these operaters all requred sequence_length
to be the same data type as the input array.

See MxNet Jira ticket here:
  https://issues.apache.org/jira/browse/MXNET-1295

See also GitHub issues here:
   apache#12649
   dmlc/gluon-nlp#346

* Adding explicit braces to an if statement to fix g++ warning

* fixing sequence_mask.cu by adding IType to template

* Fixing whitespace errors reported by linter

* Adding unit tests

* Fixing length of lines to pass linter
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
pr-awaiting-review PR is waiting for code review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants