Comments (12)
Overall, we have the chaining pattern tensor.i(...).i(...)
and macro args pattern tensor.i(ti!(..., ..., ...))
. The variable args pattern tensor.(..., ..., ...)
is not possible because Rust does not support this feature. So let's see how they could be done.
Channing pattern tensor.i(...).i(...)
The accepting type can be polymorphic so it should be simple to approach. The signature of i()
cannot simply return Tensor
. Otherwise tensor.i(..2).i(1..)
will stuck in first dimension.
It would require an intermediate type SlicedTensor
, such that both Tensor::i(T) -> SliceRecord
and SliceRecord::i(T) -> SliceRecord
can record indexes, and finally call into()
to Tensor
. Hence, it would look like this:
// long
let new_tensor = tensor
.i(3) // 1st dim select()
.i(2..5) // 2nd dim narrow()
.i(&[1, 3, 5]) // 3rd dim index_select()
.into(); // convertion
// short
let new_tensor = tensor.i(3).i(2..5).i(&[1, 3, 5]).into();
Macro arguments pattern tensor.i(ti!(..., ..., ...))
It setups a macro ti!
as a proxy, which converts user arguments into single type. Actually, I found ndarray crate ad opts this pattern. IMO, it looks more compact than chaining pattern. Unfortunately, it plays the risk of name ambiguity among crates.
let new_tensor = tensor.i(ti![3, 2..5, &[1, 3, 5]]);
from tch-rs.
That sounds like a very good idea! I looked a bit in the past at using Index
and IndexMut
but as they return references these are a bit hard to leverage, the syntax would be nicer but your tensor.i(...)
suggestion seems reasonable too.
Handling multiple dimensions via macros sound cool too. Maybe writing tensor.i(...).i(...)
would be ok to start with even if tensor.i(..., ...)
would feel a bit better.
Anyway happy to get some PR for this or more suggestions and I may have a look at it when I find some time.
from tch-rs.
Thanks for all the details!
One thing I'm not sure to understand is why would we need a ti!
macro ? I get that we cannot do tensor.i(..., ..., ...)
but what about tensor.i((..., ..., ...))
supporting tuples say up to length 4 or 5. If this was possible maybe this could avoid needing to introduce SlicedTensor
and the whole chaining pattern with the final into
conversion but I'm probably missing something here.
from tch-rs.
Suppose it accepts up to five parameters in a tuple tensor.i((A, B, C, D, E))
. That means you implement a tuple of combinations of Range*
, scalar and &[integer]
types up to fifth power. It seems impractical to enumerate all functions signatures. So I'm thinking fo workaround for this.
from tch-rs.
Couldn't traits be used to avoid the combinatorial explosion ? E.g. something like the following pseudo-code and then i
is a generic function that would work for any types implementing Indexer
and would call to_index
and do the dispatch according to the returned variant. There are certainly added difficulties that I haven't seen, maybe I should try implementing it to see what the problems would be.
enum Indexer {
Range(...),
Scalar(...),
Slice(...),
}
trait Indexer {
fn to_index(&self) -> Indexer;
}
impl Indexer for usize {
fn to_index(&self) -> Indexer {
Scalar(self)
}
}
from tch-rs.
Nice idea. I ever wondered this though but stucked at gathering them into one type.
Here is a proof-of-concept on my fork. I left the tensor-as-index part blank until understanding how NumPy-style indexing actually does this.
https://github.com/jerry73204/tch-rs/blob/tensor_indexer/src/tensor/index.rs
from tch-rs.
I can work on it occasionally because of errands these days.
@LaurentMazare if you already work it out, it's fine to pick the code from above comment.
from tch-rs.
Looks pretty nice. I haven't done anything on it yet so very happy if you can push it further and make a PR out of it. If you don't have the time I'll have a look at it on my side but there is no rush at all.
from tch-rs.
I studied numpy's manual on indexing. It seems a bunch of work there. The state the tldr, there are several distinct indexing scenarios handled differently. So I decided to make it simple and implement only basic indexing in #60 . Please take a look.
from tch-rs.
Fwiw, I've added some documentation + used the i(...)
operator in a couple examples. Overall I find it very neat to use.
from tch-rs.
Closing this as I think the current state is quite nice. Feel free to repoen if you want to discuss this more.
from tch-rs.
Fwiw, I've added some documentation + used the
i(...)
operator in a couple examples. Overall I find it very neat to use.
Now I wonder how to do it, there is no i(...)
found in version 0.7.0.
from tch-rs.
Related Issues (20)
- SDPA Flash kernel HOT 6
- when compile with tokenizers library , encounter static libcpmt.lib and dynamic msvcprt.lib conflict link error
- Load model once and run inference across threads?
- Build fails unless I downgrade `google-glog` to 0.6.0. HOT 5
- Copying Scalars from Torch to Rust? HOT 1
- Second-order utils ?
- Any plan on `at::Generator`?
- Could not be compliered. HOT 1
- Error with: Internal torch error: can't fopen
- concat doesn't support gradient HOT 1
- Double free or corruption (fasttop) HOT 1
- grads become zeros after a short period of training on metal backend
- Can we `.set_retains_grad(true)` ?
- model in rust, optimizer.step in python
- Preserving gradients with copy()? HOT 2
- Can't compile or test tch-rs HOT 3
- What if I am not using `pyo3==0.18.3`? HOT 4
- Error when building burn on Windows when upgrading to tch 0.16 HOT 2
- Can you help me setting environment variables? HOT 1
- la HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from tch-rs.