Intelligent reflecting surface (IRS) has emerged as a competitive solution to address blockage issues in millimeter wave (mmWave) and Terahertz (THz) communications due to its capability of reshaping wireless transmission environments. Nevertheless, obtaining the channel state information of IRS-assisted systems is quite challenging because of the passive characteristics of the IRS. In this paper, we consider the problem of beam training/alignment for IRS-assisted downlink mmWave/THz systems, where a multi-antenna base station (BS) with a hybrid structure serves a single-antenna user aided by IRS. By exploiting the inherent sparse structure of the BS-IRS-user cascade channel, the beam training problem is formulated as a joint sparse sensing and phaseless estimation problem, which involves devising a sparse sensing matrix and developing an efficient estimation algorithm to identify the best beam alignment from compressive phaseless measurements. Theoretical analysis reveals that the proposed method can identify the best alignment with only a modest amount of training overhead. Simulation results show that, for both line-of-sight (LOS) and NLOS scenarios, the proposed method obtains a significant performance improvement over existing state-of-art methods. Notably, it can achieve performance close to that of the exhaustive beam search scheme, while reducing the training overhead by 95%.