Skip to content

Commit

Permalink
Add ability to specify MLContext options
Browse files Browse the repository at this point in the history
  • Loading branch information
reillyeon committed Dec 22, 2023
1 parent e3fd7ab commit 3ab2d1f
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion webnn-add.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,26 @@
<input id="b" type="number" value="1"> =
<span id="c">?</span>
</p>
<p>
Device preference:
<select id="device">
<option selected value="auto">Auto</option>
<option value="cpu">CPU</option>
<option value="gpu">GPU</option>
</select>
</p>
<p>
Power preference:
<select id="power">
<option selected value="auto">Auto</option>
<option value="low-power">Low power</option>
<option value="high-performance">High performance</option>
</select>
</p>
<p>
Number of threads:
<input id="threads" type="number" value="0">
</p>
<p>
<button id="build">Build</button> <button disabled id="compute">Compute</button>
</p>
Expand All @@ -18,6 +38,9 @@
const aInput = document.getElementById('a');
const bInput = document.getElementById('b');
const cOutput = document.getElementById('c');
const deviceOption = document.getElementById('device');
const powerOption = document.getElementById('power');
const threadsOption = document.getElementById('threads');
const buildButton = document.getElementById('build');
const computeButton = document.getElementById('compute');
const outputSpan = document.getElementById('output');
Expand All @@ -29,7 +52,14 @@
try {
computeButton.disabled = true;

context = await navigator.ml.createContext();
const options = {
devicePreference: deviceOption.value,
powerPreference: powerOption.value,
numThreads: threadsOption.value,
};
console.log(options);
context = await navigator.ml.createContext(options);

const builder = new MLGraphBuilder(context);
const operandType = {dataType: 'float32', dimensions: [1]};
const a = builder.input('a', operandType);
Expand Down

0 comments on commit 3ab2d1f

Please sign in to comment.