Skip to main content
  • Home
  • Development
  • Documentation
  • Donate
  • Operational login
  • Browse the archive

swh logo
SoftwareHeritage
Software
Heritage
Archive
Features
  • Search

  • Downloads

  • Save code now

  • Add forge now

  • Help

https://github.com/ldyken53/TVCG-progiso
31 October 2024, 16:40:01 UTC
  • Code
  • Branches (3)
  • Releases (0)
  • Visits
Revision 9b4d063dbd517c8db6196fcfba97d7a4442af2c0 authored by Landon Dyken on 11 June 2024, 17:43:45 UTC, committed by Landon Dyken on 11 June 2024, 17:43:45 UTC
Handle all buffer management manually rather than automatically allocating with ONNX
1 parent 90a4a61
  • Files
  • Changes
    • Branches
    • Releases
    • HEAD
    • refs/heads/main
    • refs/heads/master
    • refs/heads/npm
    • 9b4d063dbd517c8db6196fcfba97d7a4442af2c0
    No releases to show
  • e9f1060
  • /
  • src
  • /
  • inference_session.js
Raw File Download
Take a new snapshot of a software origin

If the archived software origin currently browsed is not synchronized with its upstream version (for instance when new commits have been issued), you can explicitly request Software Heritage to take a new snapshot of it.

Use the form below to proceed. Once a request has been submitted and accepted, it will be processed as soon as possible. You can then check its processing state by visiting this dedicated page.
swh spinner

Processing "take a new snapshot" request ...

To reference or cite the objects present in the Software Heritage archive, permalinks based on SoftWare Hash IDentifiers (SWHIDs) must be used.
Select below a type of object currently browsed in order to display its associated SWHID and permalink.

  • revision
  • directory
  • content
  • snapshot
origin badgerevision badge
swh:1:rev:9b4d063dbd517c8db6196fcfba97d7a4442af2c0
origin badgedirectory badge
swh:1:dir:9a5575fd388597f53212908dc146c18fc621d17e
origin badgecontent badge
swh:1:cnt:656758e001353e044c889ee00b9db9a38e29f9b3
origin badgesnapshot badge
swh:1:snp:6dbeb1fbe859f48945dd22bd530e60eb1a5ac9b6

This interface enables to generate software citations, provided that the root directory of browsed objects contains a citation.cff or codemeta.json file.
Select below a type of object currently browsed in order to generate citations for them.

  • revision
  • directory
  • content
  • snapshot
Generate software citation in BibTex format (requires biblatex-software package)
Generating citation ...
Generate software citation in BibTex format (requires biblatex-software package)
Generating citation ...
Generate software citation in BibTex format (requires biblatex-software package)
Generating citation ...
Generate software citation in BibTex format (requires biblatex-software package)
Generating citation ...
Tip revision: 9b4d063dbd517c8db6196fcfba97d7a4442af2c0 authored by Landon Dyken on 11 June 2024, 17:43:45 UTC
Handle all buffer management manually rather than automatically allocating with ONNX
Tip revision: 9b4d063
inference_session.js
import 'jimp';
import * as ort from "onnxruntime-web/webgpu";

export class InferenceSessionWGPU {
  constructor(width, height) {
    return (async () => {
      this.width = width;
      this.height = height;
      this.inputBuffers = [null, null, null, null, null];
      this.inputTensors = [null, null, null, null, null];
      this.outputBuffers = [null, null, null, null, null, null, null];
      this.outputTensors = [null, null, null, null, null, null, null];

      try {
        this.session = await ort.InferenceSession.create(`./noof${width}.onnx`,{
          executionProviders: ['webgpu'], preferredOutputLocation: "gpu-buffer"
        });
        this.device = ort.env.webgpu.device;
      } catch (e) {
          console.log(e);
          alert("Unable to create inference session!");
      }
      
      this.inputBuffers[0] = this.device.createBuffer({
        label: `InferenceSessionWGPU.inputBuffer0`,
        size: this.width * this.height * 3 * 4,
        usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_SRC,
      });
      this.inputTensors[0] = ort.Tensor.fromGpuBuffer(this.inputBuffers[0], {
        dataType: 'float32',
        dims: [1, 3, this.height, this.width]
      });
      this.outputBuffers[0] = this.device.createBuffer({
        label: `InferenceSessionWGPU.outputBuffer0`,
        size: this.width * this.height * 3 * 4,
        usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_SRC | GPUBufferUsage.COPY_DST,
      });
      this.outputTensors[0] = ort.Tensor.fromGpuBuffer(this.outputBuffers[0], {
        dataType: 'float32',
        dims: [1, 3, this.height, this.width]
      });
      this.outputBuffers[1] = this.device.createBuffer({
        label: `InferenceSessionWGPU.outputBuffer1`,
        size: this.width * this.height * 3 * 4,
        usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_SRC | GPUBufferUsage.COPY_DST,
      });
      this.outputTensors[1] = ort.Tensor.fromGpuBuffer(this.outputBuffers[1], {
        dataType: 'float32',
        dims: [1, 3, this.height, this.width]
      });


      this.architecture = [32, 64, 64, 80, 96];
      for (var i = 0; i < this.inputBuffers.length - 1; i++) {
        var w = this.width / 2**i;
        var h = this.height / 2**i;
        this.inputBuffers[this.inputBuffers.length - 1 - i] = this.device.createBuffer({
          label: `InferenceSessionWGPU.inputBuffer${this.inputBuffers.length - 1 - i}`,
          size: this.architecture[i] * w * h * 4,
          usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_SRC | GPUBufferUsage.COPY_DST,
        }); 
        this.inputTensors[this.inputBuffers.length - 1 - i] = ort.Tensor.fromGpuBuffer(
          this.inputBuffers[this.inputBuffers.length - 1 - i], 
          {
            dataType: 'float32',
            dims: [1, this.architecture[i], h, w]
          }          
        );
      }

      for (var i = 0; i < this.outputBuffers.length - 2; i++) {
        var w = this.width / 2**i;
        var h = this.height / 2**i;
        this.outputBuffers[this.outputBuffers.length - 1 - i] = this.device.createBuffer({
          label: `InferenceSessionWGPU.outputBuffer${this.outputBuffers.length - 1 - i}`,
          size: this.architecture[i] * w * h * 4,
          usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_SRC | GPUBufferUsage.COPY_DST,
        }); 
        this.outputTensors[this.outputBuffers.length - 1 - i] = ort.Tensor.fromGpuBuffer(
          this.outputBuffers[this.outputBuffers.length - 1 - i], 
          {
            dataType: 'float32',
            dims: [1, this.architecture[i], h, w]
          }          
        );
      }
      return this;
    })();
  }

  async release() {
    for (var i = 0; i < this.inputBuffers.length; i++) {
      this.inputBuffers[i].destroy();
    }
    for (var i = 0; i < this.outputBuffers.length; i++) {
      this.outputBuffers[i].destroy();
    }
    await this.session.release();
  }

  cleanRecurrentState() {
    for (var i = 0; i < this.inputBuffers.length - 1; i++) {
      this.inputBuffers[this.inputBuffers.length - 1 - i].destroy();
      var w = this.width / 2**i;
      var h = this.height / 2**i;
      this.inputBuffers[this.inputBuffers.length - 1 - i] = this.device.createBuffer({
        label: `InferenceSessionWGPU.inputBuffer${this.inputBuffers.length - 1 - i}`,
        size: this.architecture[i] * w * h * 4,
        usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_SRC | GPUBufferUsage.COPY_DST,
      }); 
      this.inputTensors[this.inputBuffers.length - 1 - i] = ort.Tensor.fromGpuBuffer(
        this.inputBuffers[this.inputBuffers.length - 1 - i], 
        {
          dataType: 'float32',
          dims: [1, this.architecture[i], h, w]
        }          
      );
    }
  }

  async runInference() {
    // create feeds with the input name from model export and the tensors
    const feeds = {};
    for (var i = 0; i < this.inputBuffers.length; i++) {
      feeds[this.session.inputNames[i]] = this.inputTensors[i];
    }
    const fetches = {};
    for (var i = 0; i < this.outputBuffers.length; i++) {
      fetches[this.session.outputNames[i]] = this.outputTensors[i];
    }
    const start = performance.now();  
    await this.session.run(feeds, fetches);

    // var readInput = this.device.createBuffer({
    //     size: this.outputBuffers[4].size,
    //     usage: GPUBufferUsage.MAP_READ | GPUBufferUsage.COPY_DST
    // });
    // var commandEncoder = this.device.createCommandEncoder();
    // commandEncoder.copyBufferToBuffer(
    //     this.outputBuffers[4], 0, readInput, 0,
    //     readInput.size);
    // this.device.queue.submit([commandEncoder.finish()]);
    // await this.device.queue.onSubmittedWorkDone();
    // await readInput.mapAsync(GPUMapMode.READ);
    // var test = new Float32Array(readInput.getMappedRange());
    // console.log(test);
    // for (var i = 0; i < test.length; i++) {
    //   if (test[i] != 0) {
    //     console.log(i);
    //   }
    // }

    // copy recurrent state back into input
    var commandEncoder = this.device.createCommandEncoder();
    for (var i = 1; i < this.inputBuffers.length; i++) {
      commandEncoder.copyBufferToBuffer(
        this.outputBuffers[i + 2], 0, this.inputBuffers[i], 0, this.outputBuffers[i + 2].size
      );
    }
    this.device.queue.submit([commandEncoder.finish()]);
    await this.device.queue.onSubmittedWorkDone();
    const end = performance.now();

    const inferenceTime = Math.round(end - start);
    return inferenceTime;
  }
  
};


 
The diff you're trying to view is too large. Only the first 1000 changed files have been loaded.
Showing with 0 additions and 0 deletions (0 / 0 diffs computed)
swh spinner

Computing file changes ...

back to top

Software Heritage — Copyright (C) 2015–2025, The Software Heritage developers. License: GNU AGPLv3+.
The source code of Software Heritage itself is available on our development forge.
The source code files archived by Software Heritage are available under their own copyright and licenses.
Terms of use: Archive access, API— Content policy— Contact— JavaScript license information— Web API