Placeholder

class webdnn.graph.placeholder.Placeholder(value=None, label=None)[source]

Placeholder represents values which are unknown at compile time and determined at runtime, like batch size, length of time series, etc.

Placeholder supports only integer value.

Placeholder is embedded in graph descriptor and resolved at runtime like follows:

runner = await WebDNN.load('./my_model');
await runner.setPlaceholderValue({
    'N': 8
});

Also Placeholder can be resolved by setting concrete value at compile time.

>>> x = Placeholder(label="x")
>>> print(x)
<x>
>>> x.value = 3
>>> print(x)
3
>>> print(type(x))
<class 'webdnn.graph.placeholder.Placeholder'>

Placeholder supports follow basic operations.

  • add(+), sub(-), mul(*), mod(%), floor-div(//).

    >>> x = Placeholder(label="x")
    >>> y = x * 2 + 3
    >>> print(y)
    <x> * 2 + 3
    
    >>> x = Placeholder(label="x")
    >>> y = x % 4 // 5
    >>> print(y)
    (<x> % 4) // 5
    

    If possible, equation are simplified

    >>> x = Placeholder(label="x")
    >>> y = x * 6 + x * 7
    >>> print(y)
    <x> * 13
    
  • eq(==), ne(!=)

    If both placeholders are resolved, they are compared based on concrete values.

    >>> p = Placeholder(value=3)
    >>> x = p * 2
    >>> y = p + p
    >>> print(x == y == 6)
    True
    

    If either placeholder is not resolved, they are compared symbolically.

    >>> p = Placeholder(label="p")
    >>> x = p * 2
    >>> y = p * 3
    >>> print(x == y)
    False
    >>> p.value = 0
    >>> print(x == y)
    True
    
  • gt(>), lt(<), ge(>=), le(<=)

    Supported only when both placeholders are resolved. Otherwise, an error is raised.

    >>> p = Placeholder(label="p")
    >>> x = p * 2
    >>> y = p * 3
    >>> print(x < y)
    ValueError: First operand is unresolved placeholder. It can't be compared.
    >>> p.value = 3
    >>> print(x < y)
    True
    
label

str – the label.

static check_resolved(x)[source]

Check whether specified placeholder is resolved or not. :param x: the placeholder

Returns:If True, the placeholder is resolved. Otherwise, it’s not resolved.
Return type:(bool)
static force_int(x)[source]

Convert the placeholder into concrete integer value. If x is not resolved, an error is raised. :param x: the placeholder

Returns:an integer
Return type:(int)
generate_js_function(flag_semicolon=True)[source]

Generate javascript code to resolve this placeholder’s value at runtime.

Parameters:flag_semicolon (bool) – If True, semicolon is appended into generated code.
Returns:generated code
Return type:(str)
get_depend_placeholders()[source]

List up all dependent placeholders

Returns:list of all dependent placeholders
Return type:(list of Placeholder)
static to_int(x)[source]

Convert the placeholder into concrete integer value. :param x: the placeholder

Returns:If x is resolved, an integer is returned. Otherwise, x itself is returned.
Return type:(int or Placeholder)
value

The placeholder’s value. If it’s not resolved, the placeholder itself is returned.

If the placeholder is already resolved, new value cannot be set, and it causes an error.