Agent skill

numpy-interop

Stars 0
Forks 0

Install this agent skill to your Project

npx add-skill https://github.com/cuba6112/skillfactory/tree/main/skills/numpy-interop

SKILL.md

Overview

NumPy Interoperability encompasses the protocols that allow different numerical libraries (PyTorch, TensorFlow, SciPy) to exchange data without redundant copies. It focuses on modern standards like DLPack and the implementation of custom behavior for non-NumPy objects using the __array_ufunc__ protocol.

When to Use

  • Passing data from a GPU-based library (like PyTorch) back to the CPU for NumPy analysis.
  • Implementing custom array-like objects that should work seamlessly with np.sin() or np.add().
  • Interfacing with low-level C/C++ extensions using memory pointers and typestrings.
  • Avoiding memory copies when moving large tensors between frameworks.

Decision Tree

  1. Exchanging data with a modern tensor library (PyTorch)?
    • Use np.from_dlpack().
  2. Creating a custom class that needs to handle NumPy operations?
    • Implement the __array_ufunc__ method.
  3. Accessing raw memory pointers for a C extension?
    • Inspect the __array_interface__ attribute.

Workflows

  1. Converting PyTorch Tensors to NumPy

    • Ensure the PyTorch tensor is on the CPU: tensor.cpu().
    • Convert using np.from_dlpack(tensor).
    • The resulting NumPy array is a view of the tensor's memory.
  2. Overriding NumPy Ops for Subclasses

    • Implement the __array_ufunc__ method in a custom class.
    • Define how standard operations (like np.sin) should behave for your object.
    • Call np.sin(my_obj) and observe that your custom implementation is executed.
  3. Low-Level Memory Access via Interface

    • Inspect an object's __array_interface__ attribute.
    • Retrieve the 'data' pointer and 'typestr'.
    • Use these to wrap the memory in a third-party C/C++ extension for high-performance processing.

Non-Obvious Insights

  • CPU Limitation: NumPy currently only supports DLPack for CPU-resident data; GPU-resident objects must be moved to host memory before conversion.
  • Writeability Requirement: Current NumPy implementations of DLPack primarily support writeable arrays; read-only arrays may fail to export.
  • Legacy Interfaces: __array_struct__ is considered legacy; developers should prioritize the buffer protocol or DLPack for new library integrations.

Evidence

  • "DLPack is yet another protocol to convert foreign objects to NumPy arrays in a language and device agnostic manner." Source
  • "As long as foreign objects implement the array_ufunc or array_function protocols, it is possible to operate on them without the need for explicit conversion." Source

Scripts

  • scripts/numpy-interop_tool.py: Example of array_ufunc implementation and DLPack usage.
  • scripts/numpy-interop_tool.js: Simulated memory pointer inspection.

Dependencies

  • numpy (Python)

References

  • references/README.md

Expand your agent's capabilities with these related and highly-rated skills.

cuba6112/skillfactory

prompt-engineering

Comprehensive prompt engineering techniques for Claude models. Use this skill when crafting, optimizing, or debugging prompts for Claude API, Claude Code, or any Claude-powered application. Covers system prompts, role prompting, multishot examples, chain of thought, XML structuring, long context handling, extended thinking, prompt chaining, Claude 4.x-specific best practices, and agentic orchestration including subagents, agent loops, skills, MCP integration, and multi-agent workflows.

0 0
Explore
cuba6112/skillfactory

adk-rag-agent

Build RAG (Retrieval-Augmented Generation) agents with Google ADK and Vertex AI RAG Engine. Use when implementing document Q&A, knowledge base search, or citation-backed responses. Covers VertexAiRagRetrieval tool, corpus setup, and citation formatting.

0 0
Explore
cuba6112/skillfactory

headless-cli-agents

Build agentic systems using Claude CLI in headless mode or the Claude Agent SDK. Use when building automation pipelines, CI/CD integrations, multi-agent orchestration, or programmatic Claude interactions. Covers CLI flags (-p, --output-format), session management (--resume, --continue), Python SDK (claude-agent-sdk), custom tools, and agent loop patterns.

0 0
Explore
cuba6112/skillfactory

notion-knowledge-capture

Capture conversations and decisions into structured Notion pages; use when turning chats/notes into wiki entries, how-tos, decisions, or FAQs with proper linking.

0 0
Explore
cuba6112/skillfactory

mcp-builder

Guide for creating high-quality MCP (Model Context Protocol) servers that enable LLMs to interact with external services through well-designed tools. Use when building MCP servers to integrate external APIs or services, whether in Python (FastMCP) or Node/TypeScript (MCP SDK).

0 0
Explore
cuba6112/skillfactory

gh-fix-ci

Inspect GitHub PR checks with gh, pull failing GitHub Actions logs, summarize failure context, then create a fix plan and implement after user approval. Use when a user asks to debug or fix failing PR CI/CD checks on GitHub Actions and wants a plan + code changes; for external checks (e.g., Buildkite), only report the details URL and mark them out of scope.

0 0
Explore

Didn't find tool you were looking for?

Be as detailed as possible for better results