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

Swift API #19

Closed
janerivi opened this issue Nov 9, 2015 · 62 comments
Closed

Swift API #19

janerivi opened this issue Nov 9, 2015 · 62 comments
Labels
stat:contribution welcome Status - Contributions welcome

Comments

@janerivi
Copy link

janerivi commented Nov 9, 2015

Swift is a very popular and expressive language with a large community of pro developers.

@ghost
Copy link

ghost commented Nov 9, 2015

Providing APIs for every 'very popular' lang would probably bloat the main project quite quickly.

@janerivi
Copy link
Author

janerivi commented Nov 9, 2015

I understand the sentiment, but Swift, C# and Java are not "every" language, and this is not "every project" either. This is open source and Github so there might be enough hands to man the maintanance of ports to the most popular languages for such an important piece of software. Python is quite popular in academia but a little less popular with the bulk of private sector developers.

@davidzchen
Copy link
Contributor

Given that TensorFlow is designed to also run on mobile and that Apple is recommending Swift as a primary language for iOS development, I think having a Swift API for TensorFlow would be a good idea.

I don't think adding support for the most widely-used languages such as Java, C#, and Swift would be a bad idea. The gRPC project already supports 10 languages, including Java, C#, and JavaScript (via Node.js).

@jamesliu96 Feel free to open an issue for adding a Go API.

@vincentvanhoucke
Copy link
Contributor

A Swift frontend, especially for simply running graphs for inference on mobile would be great. I don't know of current plans to provide one, so feel free to dive in. Since our exact plans on accepting external contributions are still in flux, it would be a good idea to check in with the discuss mailing list with a draft of the code ahead of time to figure out where it should live exactly.

@loopbum
Copy link

loopbum commented Jan 8, 2016

I couldn't agree more with janerivi and davidzchen. Swift has many advanced features that make it one, if not the best choice for full API support for TensorFlow.

This is especially true since Swift is now open source. This language is very pleasant to work with and has all of the great features of top expressive languages like Haskell; Generics, Closures, etc.

I would not agree that C# or Java are good candidates in terms of speed. Glyn Williams is a trusted and respected contributor and writer in many areas and had this to say at:
https://www.quora.com/In-terms-of-performance-speed-is-Swift-faster-than-Java

Glyn Williams:
Swift is significantly faster than Java.
It compiles to native code. And a number of the language features enable an optimizing compiler to produce very fast code indeed.

Vivian Keating:
Swift will typically run faster than Java, since it compiles into native code via the LLVM compiler. Most recent metrics find it comparable to C++ in production.

This is the tip of the iceberg in terms of comparisons. Apple took great care in designing this modern language and how it compiles to native code, and now it's Open Source.

@rxwei
Copy link
Contributor

rxwei commented Oct 6, 2016

I'll take on this.

@johndpope
Copy link
Contributor

johndpope commented Oct 26, 2016

I have this swift docker file with parsley mcparsefsce / tensorflow + separate swift 3 dev container. - I will plugin swift grpc once google resolve an issue with their just released grpc library

https://github.com/johndpope/DockerParseyMcParsefaceAPI

I could use some help to get it all working.

Some @nubbel stubbed out proto files in swift here
https://github.com/nubbel/swift-tensorflow/tree/master/types
Should be able to leverage this environment to get above working.

@HelloFillip
Copy link

I have been working on a painful private project for a while using Swift. I'll be migrating the project API library to Swift 3 soon and opening it up under the same licensing as TensorFlow (Apache 2.0). It was reasonably complete before Swift 3 and I plan to have the Swift 3 version complete before the end of 2016, sans mistakes from original, and including lessons learned.

@johndpope
Copy link
Contributor

@siilime fyi - Google released an official swift3 grpc library last week.
https://github.com/grpc/grpc-swift
There's still some heavy lifting to do to get it all working(eg generate swift 3 services classes). You may want to revisit your implementation and yield to official library. Surely less painful in the long run. I'm aware of some other grpc libraries and wrapping the c classes into swift module.(just search on GitHub (swift + grpc) there's one by Huawei. My interest is to get it working server side. If anyone wants to help / check the issues grpc-swift for docker issues ticket.

@HelloFillip
Copy link

@johndpope Good tip. Thanks. My focus was on the server side originally, and it's where the majority of my work has been so I'll be taking a look at that.

@johndpope
Copy link
Contributor

johndpope commented Oct 31, 2016

Looks like @rxwei has made some progress here around providing some basic test cases around a swift wrapper for the tensorflow c api.

https://github.com/rxwei/tensorflow/tree/master/tensorflow/swift
https://github.com/rxwei/CTensorFlow

https://github.com/tensorflow/tensorflow/blob/master/tensorflow/c/c_api.h

@HelloFillip
Copy link

HelloFillip commented Nov 1, 2016

I've done a lot of review work over my original code base and the official Swift gRPC code base in the past few days, and although there's advantages to using the gRPC in some cases, there's still a lot missing, (specifically an easy option to train TensorFlow directly from Swift), and those gaps are the areas I'll be focusing on, whilst aiming to develop a complete solution in Swift.

Good start with @rxwei contribution so will aim to assist in that, whilst migrating existing code base.

@rxwei
Copy link
Contributor

rxwei commented Nov 1, 2016

Currently the C API is under-documented and offers only a few core components, Graph, Tensor, and Session. I start to wonder if wrapping all the C API is worthwhile.

@HelloFillip
Copy link

@rxwei What feasible alternative approaches are there?

@johndpope
Copy link
Contributor

for reference - found this - https://github.com/somaticio/tensorflow.rb
a ruby wrapper around tensorflow. (there maybe more)
It looks like they wrapped the c api in another c wrapper. Not sure if this helps.
https://github.com/somaticio/tensorflow.rb/tree/master/ext/sciruby/tensorflow_c/files

screen shot 2016-11-02 at 17 13 09

Another shot is to use grpc to interact with tensor.
but the services layer needs fleshing out which will be simpler once google can spit out service class definitions (think afnetworking for protobuf) for grpc
screen shot 2016-11-02 at 17 02 28

the python API has hundreds of granular parameters.
https://www.tensorflow.org/versions/r0.10/api_docs/python/index.html

compared to c++ API / c api.
https://www.tensorflow.org/versions/r0.10/api_docs/cc/index.html
Env

tensorflow::Env
tensorflow::RandomAccessFile
tensorflow::WritableFile
tensorflow::EnvWrapper
Session

tensorflow::Session
tensorflow::SessionOptions
Status

tensorflow::Status
tensorflow::Status::State
Tensor

tensorflow::Tensor
tensorflow::TensorShape
tensorflow::TensorShapeDim
tensorflow::TensorShapeUtils
tensorflow::PartialTensorShape
tensorflow::PartialTensorShapeUtils
Thread

tensorflow::Thread
tensorflow::ThreadOptions

If you're looking to machine learning in swift today (nov-2016) it'd pay to shop around for other libraries. https://github.com/search?utf8=%E2%9C%93&q=swift+ml&type=Repositories&ref=searchresults

One day - another (somewhat unfeasible) approach could be to rewrite the python library in swift.

@rxwei
Copy link
Contributor

rxwei commented Nov 2, 2016

@johndpope Honestly none of those Swift ML libraries are nearly ready, cross-platform, nor written in Swift 3.0+.

@rxwei
Copy link
Contributor

rxwei commented Nov 2, 2016

@johndpope service approach makes sense for now, as a wrapper.

@johndpope
Copy link
Contributor

johndpope commented Nov 2, 2016

related grpc/grpc-swift#2
also there is this swift library for those that want to get their feet? / toes wet. https://github.com/qoncept/TensorSwift

@rxwei
Copy link
Contributor

rxwei commented Nov 2, 2016

@johndpope That one is dependent on Darwin platforms and Apple's Accelerate framework. Not suitable for general use.

pooyadavoodi pushed a commit to pooyadavoodi/tensorflow that referenced this issue Oct 16, 2019
Add use_explicit_batch parameter available in OpConverterParams and other places

Formatting and make const bool everywhere

Enable use_explicit_batch for TRT 6.0

Revise validation checks to account for use_explicit_batch. Propagate flag to ConversionParams and TRTEngineOp

Rename use_explicit_batch/use_implicit_batch

Formatting

Add simple activtion test for testing dynamic input shapes. Second test with None dims is disabled

Update ConvertAxis to account for use_implicit batch

fix use of use_implicit_batch (tensorflow#7)

* fix use of use_implicit_batch

* change order of parameters in ConvertAxis function

fix build (tensorflow#8)

Update converters for ResNet50 (except Binary ops) (tensorflow#9)

* Update RN50 converters for use_implicit_batch: Conv2D, BiasAdd, Transpose, MaxPool, Squeeze, MatMul, Pad

* Fix compilation errors

* Fix tests

Use TRT6 API's for dynamic shape (tensorflow#11)

* adding changes for addnetworkv2

* add plugin utils header file in build

* optimization profile api added

* fix optimization profile

* TRT 6.0 api changes + clang format

* Return valid errors in trt_engine_op

* add/fix comments

* Changes to make sure activation test passes with TRT trunk

* use HasStaticShape API, add new line at EOF

Allow opt profiles to be set via env variables temporarily.

Undo accidental change

 fix segfault by properly returning the status from OverwriteStaticDims function

Update GetTrtBroadcastShapes for use_implicit_batch (tensorflow#14)

* Update GetTrtBroadcastShapes for use_implicit_batch

* Formatting

Update activation test

Fix merge errors

Update converter for reshape (tensorflow#17)

Allow INT32 for elementwise (tensorflow#18)

Add Shape op (tensorflow#19)

* Add Shape op

* Add #if guards for Shape. Fix formatting

Support dynamic shapes for strided slice (tensorflow#20)

Support dynamic shapes for strided slice

Support const scalars + Pack on constants (tensorflow#21)

Support const scalars and pack with constants in TRT6

Fixes/improvements for BERT (tensorflow#22)

* Support shrink_axis_mask for StridedSlice

* Use a pointer for final_shape arg in ConvertStridedSliceHelper. Use final_shape for unpack/unstack

* Support BatchMatMulV2.

* Remove TODO and update comments

* Remove unused include

* Update Gather for TRT 6

* Update BatchMatMul for TRT6 - may need more changes

* Update StridedSlice shrink_axis for TRT6

* Fix bugs with ConvertAxis, StridedSlice shrink_axis, Gather

* Fix FC and broadcast

* Compile issue and matmul fix

* Use nullptr for empty weights

* Update Slice

* Fix matmul for TRT6

* Use enqueueV2. Don't limit to 1 input per engine

Change INetworkConfig to IBuilderConfig

Allow expand dims to work on dynamic inputs by slicing shape. Catch problems with DepthwiseConv. Don't try to verify dynamic shapes in CheckValidSize (tensorflow#24)

Update CombinedNMS converter (tensorflow#23)

* Support CombinedNMS in non implicit batch mode. The squeeze will not work if multiple dimensions are unknown

* Fix compile error and formatting

Support squeeze when input dims are unknown

Support an additional case of StridedSlice where some dims aren't known

Use new API for createNetworkV2

Fix flag type for createNetworkV2

Use tensor inputs for strided slice

Allow squeeze to work on -1 dims

Add TRT6 checks to new API

spliting ConvertGraphDefToEngine  (tensorflow#29)

* spliting ConvertGraphDefToEngine into ConvertGraphDefToNetwork and BuildEngineFromNetwork

* some compiler error

* fix format

Squeeze Helper function (tensorflow#31)

* Add squeeze helper

* Fix compile issues

* Use squeeze helper for CombinedNMS

Update Split & Unpack for dynamic shapes (tensorflow#32)

* Update Unpack for dynamic shapes

* Fix compilation error

Temporary hack to fix bug in config while finding TRT library

Fix errors from rebasing

Remove GatherV2 limitations for TRT6

Fix BiasAdd elementwise for NCHW case with explicit batch mode (tensorflow#34)

Update TRT6 headers, Make tests compile (tensorflow#35)

* Change header files for TRT6 in configure script

* Fix bug with size of scalars. Use implicit batch mode based on the converter flag when creating network

* Fix compilation of tests and Broadcast tests

Properly fix biasadd nchw (tensorflow#36)

Revert tensorflow#29 to fix weight corruption (tensorflow#37)

* Revert tensorflow#29 to fix weight corruption

* Revert change in test

Fix bug with converters and get all tests passing for TRT6 (tensorflow#39)

Update DepthToSpace and SpaceToTest for TRT6 + dynamic shapes (tensorflow#40)

Add new C++ tests for TRT6 converters (tensorflow#41)

* Remove third shuffle layer since bug with transpose was fixed

* Add new tests for TRT6 features

* Update TRT6 headers list

Fix compilation errors

Remove bazel_build.sh

Enable quantization mnist test back

Disabled by mistake I believe

Remove undesirable changes in quantization_mnist_test

Add code back that was missed during rebase

Fix bug: change "type" to type_key
cjolivier01 pushed a commit to Cerebras/tensorflow that referenced this issue Dec 6, 2019
…stream-clean-bazel

Clean up bazel scripts to support both CUDA and ROCm platform
tensorflow-copybara pushed a commit that referenced this issue Aug 4, 2020
keithm-xmos referenced this issue in xmos/tensorflow Feb 1, 2021
Pad op fix for named_uint_custom_option() to get_named_uint_custom_op…
sunshinemyson added a commit to sunshinemyson/tensorflow that referenced this issue Mar 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stat:contribution welcome Status - Contributions welcome
Projects
None yet
Development

No branches or pull requests