<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Shihao Xu on Medium]]></title>
        <description><![CDATA[Stories by Shihao Xu on Medium]]></description>
        <link>https://medium.com/@xsh6528?source=rss-1a112036a995------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*dmbNkD5D-u45r44go_cf0g.png</url>
            <title>Stories by Shihao Xu on Medium</title>
            <link>https://medium.com/@xsh6528?source=rss-1a112036a995------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 04:56:46 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@xsh6528/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[[Caffe2 API] Context]]></title>
            <link>https://medium.com/@xsh6528/caffe2-api-context-f6ea58009b27?source=rss-1a112036a995------2</link>
            <guid isPermaLink="false">https://medium.com/p/f6ea58009b27</guid>
            <category><![CDATA[caffe2]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[deep-learning]]></category>
            <dc:creator><![CDATA[Shihao Xu]]></dc:creator>
            <pubDate>Sun, 29 Jul 2018 19:31:46 GMT</pubDate>
            <atom:updated>2018-07-29T19:32:31.398Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>@context.define_context(allow_default=False)</strong>.</p><p>Adding <a href="https://gist.github.com/8ef7f8ae4e77f34a06991f6867d09db5#decorator">decorator</a> @define_context() to a class will turn the class into a <a href="https://docs.python.org/3.6/reference/datamodel.html#with-statement-context-managers">With Statement Context Manager</a>.</p><p>For class that allows default, you can imagine there is always a default context instance has been automatically initialized for you. See <a href="https://gist.github.com/8ef7f8ae4e77f34a06991f6867d09db5#allow-default">example</a> below.</p><p>Classes not allowing default: TaskGroup, Task, NetBuilder.</p><p>Classes allowing default: Cluster, Node.</p><h3>Example</h3><h3>Basic</h3><p><a href="https://gist.github.com/9eb338f7781ded6ecee6bf9c8282e4fe">https://gist.github.com/9eb338f7781ded6ecee6bf9c8282e4fe</a></p><h3>Allow Default</h3><p><a href="https://gist.github.com/57dee1383fc4e9fa22defbaff7f8ae5a">https://gist.github.com/57dee1383fc4e9fa22defbaff7f8ae5a</a></p><h3>Given or Current</h3><p><a href="https://gist.github.com/429fdd6499dbf0fd44e18e685ddb8903">https://gist.github.com/429fdd6499dbf0fd44e18e685ddb8903</a></p><p>See more examples in caffe2/caffe2/python/context_test.py.</p><h3>Implementation</h3><p>caffe2/caffe2/python/context.py</p><h3>class ContextInfo</h3><p>It holds the state about which stack level the current context is at, so that the .current() method knows which context instance to return.</p><h3>class _ContextRegistry</h3><p>It is the registry of all <a href="https://l.facebook.com/l.php?u=https%3A%2F%2Fdocs.python.org%2F3.6%2Freference%2Fdatamodel.html%23with-statement-context-managers&amp;h=ATP08f903TBLdEuA4jjtfUQHK7U6p8aSirzvKttmPrUJznuD1brMURKsdCjrGA2IXh6ZCpQUsZXnuaw-XWFT0ITOq8VrQCthkiiRw6n54zuHvW0phykcox4l_ps">context-aware</a> abstractions, like Session, TaskGroup, Task and Node.</p><p>A registry works like a dict. The key of the registry is context-aware abstraction class and the value is the class&#39;s ContextInfo.</p><h3>function _context_registry()</h3><p>It returns the single instance(Singleton) of _ContextRegistry.</p><h3>decorator @define_context()</h3><ul><li>It makes the class it decorates context-aware, by adding methods .__enter__(), .__exit__() and .current() to the class it decorates.</li></ul><p>For example, if a class MyContext is decorated with context.define_context(). MyContext becomes context-aware, meaning current MyContext could be retrieved within with scope without having to keep the instance. <a href="https://gist.github.com/772a16ac9d8fcfbe1f5b92e7802ff096">https://gist.github.com/772a16ac9d8fcfbe1f5b92e7802ff096</a></p><p>This context switch is implemented in __enter__ and __exit__.</p><ul><li>It registers the class it decorates at the _ContextRegistry singleton, by adding an _ContextInfo entry which is for tracking the current level of context.</li><li>It ensures a class is only registered as a context once.</li><li>It merges the original __enter__ and __exit__ of the class it decorated.</li><li>It makes the class it decorates a decorator, by adding methods .__call__() to the class it decorates.</li></ul><h3>Decorator</h3><p><a href="https://gist.github.com/ae7a1901b0cd5b28d362af3c9cf2039b">https://gist.github.com/ae7a1901b0cd5b28d362af3c9cf2039b</a></p><p>is equivalent to <a href="https://gist.github.com/14252e3214c5e8df274f1acbd40f9bcf">https://gist.github.com/14252e3214c5e8df274f1acbd40f9bcf</a></p><p>Reference: <a href="http://book.pythontips.com/en/latest/decorators.html">http://book.pythontips.com/en/latest/decorators.html</a></p><h3>Multi-threading</h3><p>Since threads could be at different lines of the same code, each thread should keep an independent context stack for itself.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f6ea58009b27" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Caffe2 API]]></title>
            <link>https://medium.com/series/caffe2-api-c90ba9a16b0e?source=rss-1a112036a995------2</link>
            <guid isPermaLink="false">https://medium.com/p/c90ba9a16b0e</guid>
            <dc:creator><![CDATA[Shihao Xu]]></dc:creator>
            <pubDate>Sun, 29 Jul 2018 18:59:59 GMT</pubDate>
            <atom:updated>2018-07-29T18:59:59.188Z</atom:updated>
            <content:encoded><![CDATA[<p># API</p><p>**@context.define_context(allow_default=False)**.</p><p>Adding [decorator](#decorator) `<a href="http://twitter.com/define_context">@define_context</a>()` to a class will turn the class into a [With Statement Context Manager](<a href="https://docs.python.org/3.6/reference/datamodel.html#with-statement-context-managers">https://docs.python.org/3.6/reference/datamodel.html#with-statement-context-managers</a>).</p><p>For class that allows default, you can imagine there is always a default context instance has been automatically initialized for you. See [example](#allow-default) below.</p><p>Classes not allowing default: TaskGroup, Task, NetBuilder.</p><p>Classes allowing default: Cluster, Node.</p><p># Example</p><p>## Basic</p><p>```<br>from caffe2.python import context</p><p><a href="http://twitter.com/context">@context</a>.define_context()<br>class MyContext:<br> pass</p><p>with MyContext() as ctx_1:<br> MyContext.current() # Returns ctx_1.<br> with MyContext() as ctx_2:<br> MyContext.current() # Returns ctx_2.</p><p># Try to return the “given” value first. If None is given, then return the “current” value.<br> MyContext.current(value=ctx_1) # If ctx_1 is not None, return ctx_1 (given).<br> ctx_1 = None<br> MyContext.current(value=ctx_1) # If ctx_1 is None, return ctx_2 (current).<br>```</p><p>## Allow Default</p><p>```<br>from caffe2.python import context</p><p><a href="http://twitter.com/context">@context</a>.define_context(allow_default=True)<br>class MyContext:<br> pass</p><p># You can imagine there is always a default MyContext instance has been automatically initialized for you.<br>MyContext.current() # Return that default MyContext instance.<br>```</p><p>## Given or Current<br>```<br>from caffe2.python import context</p><p><a href="http://twitter.com/context">@context</a>.define_context(allow_default=True)<br>class MyContext:<br> pass</p><p><a href="http://twitter.com/MyContext">@MyContext</a>()<br>def original_func(self): <br> MyContext.current() # Return the MyContext instance.<br># The MyContext instance could be used as a decorator.<br># Equivalent to `decorated_func = MyContext()(original_func)`<br>```</p><p>See more examples in `caffe2/caffe2/python/context_test.py`.</p><p># Implementation</p><p>## Code</p><p>`caffe2/caffe2/python/context.py`</p><p>## class `ContextInfo`</p><p>It holds the state about which stack level the current context is at, so that the `.current()` method knows which context instance to return.</p><p>## class `_ContextRegistry`</p><p>It is the registry of all [context-aware](<a href="https://l.facebook.com/l.php?u=https%3A%2F%2Fdocs.python.org%2F3.6%2Freference%2Fdatamodel.html%23with-statement-context-managers&amp;h=ATP08f903TBLdEuA4jjtfUQHK7U6p8aSirzvKttmPrUJznuD1brMURKsdCjrGA2IXh6ZCpQUsZXnuaw-XWFT0ITOq8VrQCthkiiRw6n54zuHvW0phykcox4l_ps">https://l.facebook.com/l.php?u=https%3A%2F%2Fdocs.python.org%2F3.6%2Freference%2Fdatamodel.html%23with-statement-context-managers&amp;h=ATP08f903TBLdEuA4jjtfUQHK7U6p8aSirzvKttmPrUJznuD1brMURKsdCjrGA2IXh6ZCpQUsZXnuaw-XWFT0ITOq8VrQCthkiiRw6n54zuHvW0phykcox4l_ps</a>) abstractions, like `Session`, `TaskGroup`, `Task` and `Node`.</p><p>A registry works like a dict. The key of the registry is context-aware abstraction `class` and the value is the class’s `ContextInfo`.</p><p>## function _context_registry()</p><p>It returns the single instance(Singleton) of `_ContextRegistry`.</p><p>## decorator <a href="http://twitter.com/define_context">@define_context</a>()</p><p>- It makes the class it decorates context-aware, by adding methods `.__enter__()`, `.__exit__()` and `.current()` to the class it decorates.</p><p>For example, if a class `MyContext` is decorated with `context.define_context()`.<br>`MyContext` becomes context-aware, meaning current `MyContext` could be retrieved within `with` scope without having to keep the instance.<br>```<br>with MyContext() as ctx_1: # No need to keep ctx_1.<br> MyContext.current() # Returns ctx_1.<br>```<br>This context switch is implemented in `__enter__` and `__exit__`.</p><p>- It registers the class it decorates at the `_ContextRegistry` singleton, by adding an `_ContextInfo` entry which is for tracking the current level of context.</p><p>- It ensures a class is only registered as a context once.</p><p>- It merges the original `__enter__` and `__exit__` of the class it decorated.</p><p>- It makes the class it decorates a decorator, by adding methods `.__call__()` to the class it decorates.</p><p>## Decorator</p><p>```<br><a href="http://twitter.com/context">@context</a>.define_context()<br>class A():<br> pass<br>```<br>is equivalent to<br>```<br>class A():<br> pass<br>A = context.define_context()(A)<br>```<br>Reference: <a href="http://book.pythontips.com/en/latest/decorators.html">http://book.pythontips.com/en/latest/decorators.html</a></p><p>## Multi-threading</p><p>Since threads could be at different lines of the same code, each thread should keep an independent context stack for itself.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c90ba9a16b0e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Caffe2]]></title>
            <link>https://medium.com/series/caffe2-aad00f6abb12?source=rss-1a112036a995------2</link>
            <guid isPermaLink="false">https://medium.com/p/aad00f6abb12</guid>
            <dc:creator><![CDATA[Shihao Xu]]></dc:creator>
            <pubDate>Sat, 12 May 2018 19:31:00 GMT</pubDate>
            <atom:updated>2018-05-12T19:31:00.181Z</atom:updated>
            <content:encoded><![CDATA[<p>Caffe2 API</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=aad00f6abb12" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>