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

int8 and uint8 support in Pad and other ops #387

Merged
merged 3 commits into from
Oct 17, 2024

Conversation

igor-yusupov
Copy link
Contributor

@igor-yusupov igor-yusupov commented Oct 16, 2024

@igor-yusupov igor-yusupov changed the title int8 and uint8 support in Pad and other ops WIP: int8 and uint8 support in Pad and other ops Oct 16, 2024
@igor-yusupov igor-yusupov changed the title WIP: int8 and uint8 support in Pad and other ops int8 and uint8 support in Pad and other ops Oct 16, 2024
.DS_Store Outdated
Copy link
Owner

Choose a reason for hiding this comment

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

Can you remove this macOS-specific .DS_Store file. I suggest creating a global .gitignore for this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. Sorry, I accidentally pushed that file. I also updated .gitignore file.
I think we need to make squash commit to not have this file in git history

Input::Int8Tensor(t) => Ok(t.map_in(pool, |x| *x as f32).into()),
Input::UInt8Tensor(t) => Ok(t.map_in(pool, |x| *x as f32).into()),
},
DataType::Int8 => match input {
Copy link
Owner

Choose a reason for hiding this comment

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

I think that using as for casts is fine for all cases here. Just making some notes for myself:

Converting non-i8 types to i8 will truncate in various ways, and the same for u8. The Cast op specs are here. The main cases of interest are:

  • Float -> int when out of range: ONNX spec says undefined, so an as cast is fine
  • Int -> Int when out of range: ONNX spec says when OOR, discard higher bits and reinterpret (with respect to two’s complement representation for signed types). For example, 200 (int16) -> -56 (int8).. This matches the behavior of as in Rust

.gitignore Outdated
@@ -12,3 +12,5 @@ dist/
# Converted rten ML models
*.rten
models/

*.DS_Store
Copy link
Owner

Choose a reason for hiding this comment

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

I think there was a misunderstanding. OS/editor-specific ignores shouldn't be part of this project's .gitignore. Rather they belong in your personal global gitignore file. See the blog post I linked to in the earlier comment for how to set that up.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I got it. I have updated global gitignore on my machine and restored the .gitignore file in the repository to its original state. Thank you

@robertknight
Copy link
Owner

Some notes for myself on float-to-int cast compatibility hazards:

The Cast spec says that the behavior of floating point -> integer casts are undefined if out of range. From testing numpy / PyTorch it appears that they have the same behavior as static_cast<int>(some_float) in C++. This behavior can vary depending on the platform and on wasm32 can trap. Rust's as on the other hand guarantees saturation at the cost of a potential performance impact. See rust-lang/rust#67058 and rust-lang/rust#10184.

Saturation seems like it would probably be the most useful failure handling and is cross platform. Sticking with as seems sensible unless performance turns out to be a major issue.

@robertknight
Copy link
Owner

Note for the changelog: This adds u8 and i8 support to:

  • Cast
  • Gather
  • GatherElements
  • GatherND
  • ScatterElements
  • ScatterND
  • Expand
  • Flatten
  • Reshape
  • Squeeze
  • Transpose
  • Unsqueeze
  • Pad

Copy link
Owner

@robertknight robertknight left a comment

Choose a reason for hiding this comment

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

Thanks!

@robertknight robertknight merged commit b76661c into robertknight:main Oct 17, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants