<?xml version="1.0" encoding="UTF-8"?>

<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" >

  <channel>
    <title><![CDATA[Комментарии / Профиль ipoder]]></title>
    <link>https://habr.com/ru/users/ipoder/comments/</link>
    <description><![CDATA[Хабр: комментарии пользователя ipoder]]></description>
    <language>ru</language>
    <managingEditor>editor@habr.com</managingEditor>
    <generator>habr.com</generator>
    <pubDate>Tue, 10 Nov 2026 07:15:20 GMT</pubDate>
    
    
      <image>
        <link>https://habr.com/ru/</link>
        <url>https://habrastorage.org/webt/ym/el/wk/ymelwk3zy1gawz4nkejl_-ammtc.png</url>
        <title>Хабр</title>
      </image>
    

    
      

      
        
  
    <item>
      <title>07.06.2020 08:44:14</title>
      <guid isPermaLink="true">https://habr.com/ru/companies/piter/articles/505590/#comment_21709410</guid>
      <link>https://habr.com/ru/companies/piter/articles/505590/#comment_21709410</link>
      <description><![CDATA[Забавно, но перенесённой на Torch и немного оптимизированной версии решения от <a href="https://habr.com/ru/users/physmatik/" class="user_link">Physmatik</a> уже требуется всего 0.5 секунды на GPU (1080Ti) и 10.5 секунд на CPU (TR 1920X) на обработку тех же 10^5 векторов. Учитывая возможность эффективного использования FP16 на карте V100, то на ней будет ещё быстрее.<br>
<div class="spoiler" role="button" tabindex="0">
                        <b class="spoiler_title">Код на Torch</b>
                        <div class="spoiler_text"><pre><code class="python">import torch

n = 10**5
k = 59
batch_size = 10**3
device = 'cuda:0' # 'cpu'
nn_number = 3

items = np.random.rand(n, k).astype(np.float32)
items = items / np.sqrt(np.sum(items**2, axis=1, keepdims=True))

def three_closest(X, indices):
    sample_n = indices.shape[0]
    index = torch.zeros((sample_n, nn_number), dtype=torch.int64, device=device)
    distances = X[indices, :] @ X.T
    ind_range = torch.arange(sample_n, device=device)
    distances[ind_range, indices] = 0
    for j in range(nn_number):
        max_index = torch.argmax(distances, dim=1)
        index[:, j] = max_index
        distances[ind_range, max_index] = 0
    return index

def distance_func(X):
    X = torch.from_numpy(X).to(device=device)
    all_items = torch.zeros((n, nn_number), dtype=torch.int64, device=device)
    for indices in torch.split(torch.arange(n, device=device), batch_size):        
        all_items[indices] = three_closest(X, indices)
    return all_items.cpu().numpy()

nearest_torch = distance_func(items)
</code></pre><br>
</div>
                    </div>]]></description>
      <pubDate>Sun, 07 Jun 2020 08:44:14 GMT</pubDate>
      
    </item>
  

  
    <item>
      <title>07.06.2020 05:54:57</title>
      <guid isPermaLink="true">https://habr.com/ru/companies/piter/articles/505590/#comment_21709130</guid>
      <link>https://habr.com/ru/companies/piter/articles/505590/#comment_21709130</link>
      <description><![CDATA[Считается именно 20 секунд, а не 0.5 секунд (на самом деле 0.5 мс будет выведено, перезапустил код локально). Автор посчитал затраченное время на асинхронный вызов.<br>
Также хочется отметить, число колонок 59 — очень неудачное. Если добавить ещё 5 (например, нулевых) до 64, то выполняться на gpu будет быстрее примерно в два раза.<br>
<br>
И, конечно, стоило бы воспользоваться готовой библиотекой для этой задачи. Например, при помощи <a href="https://github.com/facebookresearch/faiss">faiss</a> те же 10^5 векторов можно обработать за 25 секунд на TR 1920x (против 50 секунд для кода из статьи на 1080Ti). Результаты совпадают.<br>
<div class="spoiler" role="button" tabindex="0">
                        <b class="spoiler_title">Код</b>
                        <div class="spoiler_text"><pre><code class="python">import numpy as np
import faiss


n = 10**5
k = 59
batch_size = 18

items = np.random.rand(n, k).astype(np.float32)
items = items / np.sqrt(np.sum(items**2, axis=1, keepdims=True))
bounds = np.linspace(0, n, n // batch_size + 1, dtype=np.int64)

index = faiss.IndexFlatIP(59)
index.add(items)

distance = np.zeros((n, 4), dtype=np.float32)
index_val = np.zeros((n, 4), dtype=np.int64)
for lower, upper in zip(bounds[:-1], bounds[1:]):
    distance[lower:upper], index_val[lower:upper] = index.search(items[lower:upper], 4)
assert np.sum(index_val[:, 0] != np.arange(n)) == 0
distance = distance[:, 1:]
index_val = index_val[:, 1:]
</code></pre><br>
</div>
                    </div>]]></description>
      <pubDate>Sun, 07 Jun 2020 05:54:57 GMT</pubDate>
      
    </item>
  


      

      

    
  </channel>
</rss>
