You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am trying to generate statically-typed wrappers for Python libraries, particularly TensorFlow (see Gradient).
Since I am automatically generating .NET-side types for wrappers, I can make their methods to use my custom converter written in C# to convert function arguments to PyObject instances. And then for any result values, I can convert PyObject instances back into my wrapper types.
Problem I am facing is when a user of my wrapper wants to inherit from one of the Python-derived classes, and override and/or extend its behavior.
An artificial sample:
// this part of my library is auto-generated// represents tensorflow moduleclasstf{staticdynamictf=Py.Import("tensorflow");// set_default_session - made up methodstaticvoidSetDefaultSession(Sessionsession)=>tf.set_default_session(session.underlyingSession);// made up methodstaticvoidComputeUsingDefaultSession()=>tf.compute_using_default_session();}// represents tf.TensorclassTensor{PyObjectunderlyingTensor;}// represents tf.SessionclassSession{PyObjectunderlyingSession;virtualobjectRun(Tensortensor){varpyTensor=ConvertToPyObject(tensor);// simply does tensor.underlyingTensor heredynamicpySession=this.underlyingSession;varresult=pySession.Run(pyTensor);returnConvertFromPyObject(result);// wraps any returned object into one of generated classes}}// this code is what user of my library wants to do:classMyBetterSession:Session{overrideobjectRun(Tensortensor){
... here he writes custom code to run a Tensor ...}}// an attempts to use the above (functions made up):varsimpleSession=newSession();tf.SetDefaultSession(simpleSession);// OKtf.ComputeUsingDefaultSession();// SUCCEEDSvarbetterSession=newMyBetterSession();tf.SetDefaultSession(betterSession);// OKtf.ComputeUsingDefaultSession();// FAILS
The last line will fail, because Python will attempt to call MyBetterSession.Run with Python's class tf.Tensor, but the method actually expects wrapped class Tensor. I need to somehow tell Python or rather Python.NET to invoke my ConvertFromPyObject on the argument, before trying to find a matching overload.
I looked into Python.NET source, and the corresponding objects are MethodBinding and MethodObject, however, neither seem to provide any extensibility.
Now I am considering several approaches to the problem, and I just wanted to discuss them with Python.NET team, as some of them involve modification of Python.NET.
Do not change anything in Python.NET, and for every user class like MyBetterSession generate a pythonic wrapper with the same set of methods, but replacing all parameter and return types with PyObject. It would require one of the following:
System.Reflection.Emit, which is a very heavy dependency, and also not very pleasant to work with
Roslyn, which is much heavier, but moderately OK to work with
I noticed, that while Python.NET supports representing any Python object as dynamic, it does not actually support passing dynamic objects (e.g. IDynamicMetaObjectProvider instances) back to Python. I mean, it would pass them, but if Python would try to access a dynamic attribute, it would not attempt to call TryGetMember. I could potentially implement something similar to ClassBase specifically for wrapping IDynamicMetaObjectProvider instances. Then it is much easier to implement its members, that would simply wrap PyObject arguments before forwarding them to an instance of MyBetterSession.
Have Python.NET directly expose some low-level interface, that would enable hooking into Python.NET's marshaling and, possibly, also method binding processes.
The text was updated successfully, but these errors were encountered:
I'd be in favour of option 3), as redesigning this to be extensible would probably allow us to fix a ton of open bugs that are related to either too eager or incomplete conversions.
Regarding 2), I've implemented this in a hacky way from Python's side for classes deriving directly from DynamicObject here: #72. If one moves this to C#, it should be easy enough to extend it to IDynamicMetaObjectProvider, the only reason this hack only works for DynamicObject is because you can't inject base classes into Python.NET provided interfaces.
Environment
Details
I am trying to generate statically-typed wrappers for Python libraries, particularly TensorFlow (see Gradient).
Since I am automatically generating .NET-side types for wrappers, I can make their methods to use my custom converter written in C# to convert function arguments to
PyObject
instances. And then for any result values, I can convertPyObject
instances back into my wrapper types.Problem I am facing is when a user of my wrapper wants to inherit from one of the Python-derived classes, and override and/or extend its behavior.
An artificial sample:
The last line will fail, because Python will attempt to call
MyBetterSession.Run
with Python's classtf.Tensor
, but the method actually expects wrapped classTensor
. I need to somehow tell Python or rather Python.NET to invoke myConvertFromPyObject
on the argument, before trying to find a matching overload.I looked into Python.NET source, and the corresponding objects are
MethodBinding
andMethodObject
, however, neither seem to provide any extensibility.Now I am considering several approaches to the problem, and I just wanted to discuss them with Python.NET team, as some of them involve modification of Python.NET.
MyBetterSession
generate a pythonic wrapper with the same set of methods, but replacing all parameter and return types withPyObject
. It would require one of the following:System.Reflection.Emit
, which is a very heavy dependency, and also not very pleasant to work withI noticed, that while Python.NET supports representing any Python object as
dynamic
, it does not actually support passingdynamic
objects (e.g.IDynamicMetaObjectProvider
instances) back to Python. I mean, it would pass them, but if Python would try to access a dynamic attribute, it would not attempt to callTryGetMember
. I could potentially implement something similar toClassBase
specifically for wrappingIDynamicMetaObjectProvider
instances. Then it is much easier to implement its members, that would simply wrap PyObject arguments before forwarding them to an instance ofMyBetterSession
.Have Python.NET directly expose some low-level interface, that would enable hooking into Python.NET's marshaling and, possibly, also method binding processes.
The text was updated successfully, but these errors were encountered: